import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/module_permissions_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/major_hazard_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/securtity_base_drawer.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/archives/archives_view.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/assessment/assess_type.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/assessment/assessment_view.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_type.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_view.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/obligation_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/obligation_type.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/obligation_view.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class MajorHazardController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 展开收起控制key
  final scaffoldKey = GlobalKey<ScaffoldState>();

  /// 选中的页面
  int selectIndex = 0;

  // 标题列表
  final titleList = [
    // SecurityBaseEntity(
    //   title: '总览',
    //   icon: AppImages.icon_risk_overview,
    //   expandTitle: '总览',
    //   currentPageTitle: 0,
    //   pages: [Container()],
    //   path: '/home'
    // ),
    SecurityBaseEntity(
      title: '包保责任',
      icon: AppImages.icon_insurance_liability,
      pageTitle: ['主要负责人', '技术负责人', '操作负责人'],
      expandTitle: '包保责任管理',
      currentPageTitle: 0,
      pages: [
        HazardObligationsPage(),
        HazardObligationsPage(),
        HazardObligationsPage(),
      ],
      path: '/obligation'
    ),
    SecurityBaseEntity(
      title: '在线监测',
      icon: AppImages.icon_online_monitoring,
      pageTitle: ['DCS', 'GDS', '摄像头'],
      expandTitle: '在线监测预警',
      currentPageTitle: 0,
      pages: [
        HazardMonitorPage(type: HazardMonitorType.dcs),
        HazardMonitorPage(type: HazardMonitorType.gds),
        HazardMonitorPage(type: HazardMonitorType.camera),
      ],
      path: '/monitor'
    ),
    SecurityBaseEntity(
      title: '评价/评估',
      icon: AppImages.icon_overview_unit,
      pageTitle: ['评价报告', '评估报告'],
      expandTitle: '评价/评估',
      currentPageTitle: 0,
      pages: [
        HazardAssessmentPage(type: HazardAssessmentType.evaluation),
        HazardAssessmentPage(type: HazardAssessmentType.assessment),
      ],
      path: '/assessment'
    ),
    SecurityBaseEntity(
      title: '档案管理',
      icon: AppImages.icon_file_management,
      expandTitle: '档案管理',
      currentPageTitle: 0,
      pages: [HazardArchivesPage()],
      path: '/archives'
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    initModule();
  }

  /// 根据后台权限初始化菜单
  void initModule() {
    ModulePermissionsUtil.removeRoleMenu(titleList: titleList, routes: AppRoutes.majorHazardPage);
    update();
  }


  /// 点击左侧菜单
  /// [index] 索引
  void selectLeftMenu(int index) {
    selectIndex = index;
    selectedHazard = null;
    update();
  }

  /// title选择切换
  /// [titleIndex] 标题索引
  /// [index] 页面索引
  void selectTitle(int titleIndex, int index) {
    titleList[titleIndex].currentPageTitle = index;
    update();

    try {
      switch (titleIndex) {
        case 1:
          final ctrl = Get.find<HazardObligationsController>();
          ctrl.type = HazardObligationType.values[index];
          ctrl.getMissions();
          ctrl.getPersons();
        case 2:
          final ctrl = Get.find<HazardMonitorController>();
          ctrl.type = HazardMonitorType.values[index];
          ctrl.getData();
      }
    } catch (_) {}
  }

  /// 点击展开菜单
  /// [index] 索引
  void selectExpandMenu(int index) {
    selectIndex = index;
    Get.back();
    update();
  }

  final majorHazardList = <MajorHazardEntity>[];
  MajorHazardEntity? selectedHazard;

  @override
  void onReady() {
    super.onReady();
    init();
  }

  Future<void> init() async {
    final res = await HttpUtil()
        .generateGetRequest<List<MajorHazardEntity>>(
          Interface.getHazardList,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        majorHazardList.clear();
        majorHazardList.addAll(result.data!);
      }
    });
  }

  void showHazardSelect(Function() callback) {
    CustomSelectFunction.selectMultipleBottomSheet<MajorHazardEntity>(
      onCallBack: (val) {
        selectedHazard = val.first;
        update();
        callback();
      },
      dataList: majorHazardList,
      checkedList: selectedHazard == null ? [] : [selectedHazard!],
      isMultiple: false,
    );
  }
}
